package service

import (
	"astgram/common"
	"astgram/dal/repo"
	"astgram/model"
	"astgram/model/vo"
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
)

type LoginService struct {
}

func (ss *LoginService) checkParam(c *gin.Context, req *model.RegRequest) error {
	return nil
}

func (ss *LoginService) Execute(c *gin.Context, req *model.LoginRequest) (*model.RegResponse, error) {
	userRepo := repo.NewUserRepo()
	userPo, err := userRepo.GetUser(repo.GetUserQuery{
		Username: req.Username,
		Pwd:      req.Pwd,
	})
	if err != nil {
		return nil, err
	}
	if userPo == nil {
		return nil, errors.New("用户名或密码错误")
	}
	return &model.RegResponse{
		Meta: common.MetaOk,
		User: &vo.User{
			Username: userPo.Username,
			HeadURL:  userPo.HeadURL,
			UserID:   cast.ToInt64(userPo.ID),
		},
	}, nil
}
